package cn.loan.controller;

import cn.loan.config.key.SessionKey;
import cn.loan.config.key.StatusCode;
import cn.loan.entity.User;
import cn.loan.service.InvitationCodeService;
import cn.loan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 10:20 2019/3/20
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private InvitationCodeService invitationCodeService;


    /**
     * 去登录页面
     *
     * @return 登录页面
     */
    @GetMapping("/login")
    public String login() {
        return "/login";
    }


    /**
     * 处理登录请求
     *
     * @param session
     * @param user    用户名密码
     * @return 返回登录状态
     */
    @PostMapping("/login.do")
    @ResponseBody
    public String doLogin(HttpSession session, User user) {
        User loginUser = userService.login(user);
        if (null != loginUser) {
            //将登录成功的用户存储在session中
            session.setAttribute(SessionKey.SESSION_USER, loginUser);
            //登录成功
            return StatusCode.STATUS_SUCCESS;
        } else {
            //登录失败
            return StatusCode.STATUS_FAIL;
        }
    }

    /**
     * 去找回密码页面
     *
     * @return 找回密码页面
     */
    @GetMapping("/retrieve")
    public String retrievePassword() {
        return "/retrieve";
    }


    /**
     * 找回密码
     *
     * @param user        用户名加新密码
     * @param massageCode 用户填写的验证码
     * @param session
     * @return 返回是否修改成功的状态
     */
    @PostMapping("/retrieve.do")
    @ResponseBody
    public String doRetrieve(User user, String massageCode, HttpSession session) {
        try {
            //从session中得到验证码
            String sessionCode = (String) session.getAttribute(user.getUser_number());
            //如果session中的验证码与用户输入的验证码匹配
            if (null!=sessionCode){
                if (massageCode.equals(sessionCode)) {
                    //修改密码,true=成功,false=失败
                    boolean flag = userService.modifyPwd(user);
                    //如果修改成功
                    if (flag) {
                        session.invalidate();
                        return StatusCode.STATUS_SUCCESS;
                    } else {
                        //将验证码失效
                        session.removeAttribute(user.getUser_number());
                        return StatusCode.STATUS_FAIL;
                    }
                }
            }
            //验证码不匹配
            return StatusCode.STATUS_MISMATCH;
        } catch (Exception e) {
            //将验证码失效
            session.removeAttribute(user.getUser_number());
            e.printStackTrace();
            //异常
            return StatusCode.STATUS_ERROR;
        }
    }


    /**
     * 去注册页面
     *
     * @return 注册页面
     */
    @GetMapping("/register")
    public String register() {
        return "/register";
    }


    @PostMapping("/register.do")
    @ResponseBody
    public String doRegister(User user, String massageCode, HttpSession session) {
        try {
            System.out.println(user.getUser_number());
            //得到验证码
            String sessionCode = (String) session.getAttribute(user.getUser_number());
            //如果验证码匹配
            if (null!=sessionCode){
                if (massageCode.equals(sessionCode)) {
                    //得到手机号
                    String phone=user.getUser_number();
                    //得到随机数
                    String ram=invitationCodeService.getCode().getCode();
                    //用手机号和随机数的后三位生成邀请码
                    String code=phone.substring(phone.length()-3)+ram;
                    //邀请码
                    user.setCode(code);
                    //注册时间
                    user.setRegister_time(new Timestamp(System.currentTimeMillis()));
                    //注册
                    boolean flag = userService.register(user);
                    //注册成功
                    if (flag){
                        //验证码失效
                        session.removeAttribute(user.getUser_number());
                        return StatusCode.STATUS_SUCCESS;
                    }else {
                        //验证码失效
                        session.removeAttribute(user.getUser_number());
                        return StatusCode.STATUS_FAIL;
                    }
                }
            }
            //验证码输入不匹配
            return StatusCode.STATUS_MISMATCH;
        } catch (Exception e) {
            //将验证码失效
            session.removeAttribute(user.getUser_number());
            e.printStackTrace();
            //返回发生异常的状态码
            return StatusCode.STATUS_ERROR;
        }
    }


    /**
     * 查询邀请码存不存在
     * @param code 邀请码
     * @return 返回结果
     */
    @RequestMapping("/userCode.do")
    @ResponseBody
    public String userCode(String code){
        try{
            //去查询邀请码是否存在
            boolean flag = userService.findCode(code);
            if (flag){
                //存在返回exist
                return StatusCode.STATUS_EXIST;
            }
            //否则返回not_exist
            return StatusCode.STATUS_NOT_EXIST;
        } catch (Exception e){
            e.printStackTrace();
            //出现错误返回error
            return StatusCode.STATUS_ERROR;
        }
    }

}
